<template>
  <div class="list-table">
    <div class=" area-header">
      <span class="area-header-title">{{ $t("代码生成") }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('表名称')" prop="tableName">
        <el-input
          v-model="queryParams.tableName"
          :placeholder="$t('表名称')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('表描述')" prop="tableComment">
        <el-input
          v-model="queryParams.tableComment"
          :placeholder="$t('表描述')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('创建时间')">
        <DatePicker
          v-model="dateRange"
          type="daterange"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['tool:gen:code']"
          type="primary"
          icon="el-icon-download"
          size="mini"
          @click="handleGenTable"
        >{{ $t('生成') }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['tool:gen:import']"
          type="info"
          icon="el-icon-upload"
          size="mini"
          @click="openImportTable"
        >{{ $t('导入') }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['tool:gen:remove']"
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >{{ $t('删除') }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="tableList" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column
        :label="$t('表名称')"
        align="center"
        prop="tableName"
        :show-overflow-tooltip="true"
        width=""
      />
      <el-table-column
        :label="$t('表描述')"
        align="center"
        prop="tableComment"
        :show-overflow-tooltip="true"
        width=""
      />
      <el-table-column
        :label="$t('实体')"
        align="center"
        prop="className"
        :show-overflow-tooltip="true"
        width=""
      />
      <el-table-column :label="$t('创建时间')" align="center" prop="createTime" show-overflow-tooltip width="150" />
      <el-table-column :label="$t('更新时间')" align="center" prop="updateTime" show-overflow-tooltip width="150" />
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding" width="350">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['tool:gen:preview']"
            type="text"
            size="small"
            icon="el-icon-view"
            @click="handlePreview(scope.row)"
          >{{ $t('预览') }}</el-button>
          <el-button
            v-hasPermi="['tool:gen:edit']"
            type="text"
            size="small"
            icon="el-icon-edit"
            @click="handleEditTable(scope.row)"
          >{{ $t('编辑') }}</el-button>
          <el-button
            v-hasPermi="['tool:gen:remove']"
            type="text"
            size="small"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >{{ $t('删除') }}</el-button>
          <el-button
            v-hasPermi="['tool:gen:code']"
            type="text"
            size="small"
            icon="el-icon-download"
            @click="handleGenTable(scope.row)"
          >{{ $t('生成代码') }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 预览界面 -->
    <el-dialog :title="$t('代码预览')" :visible.sync="preview.open" width="80%" top="5vh" :close-on-click-modal="false" :lock-scroll="false">
      <el-tabs v-model="preview.activeName">
        <el-tab-pane
          v-for="(value, key) in preview.data"
          :key="key"
          :label="key.substring(key.lastIndexOf('/')+1,key.indexOf('.vm'))"
          :name="key.substring(key.lastIndexOf('/')+1,key.indexOf('.vm'))"
        >
          <pre>{{ value }}</pre>
        </el-tab-pane>
      </el-tabs>
    </el-dialog>
    <!-- 编辑界面 -->
    <el-dialog :title="$t('配置')" :visible.sync="editOpen" width="80%" top="5vh" :close-on-click-modal="false" :lock-scroll="false">
      <editTable ref="editTable" />
    </el-dialog>

    <import-table ref="import" @ok="handleQuery" />
  </div>
</template>

<script>
import { listTable, previewTable, delTable } from '@/api/tool/gen'
import importTable from './importTable'
import editTable from './editTable'
import { downLoadZip } from '@/utils/zipdownload'
export default {
  name: 'Gen',
  components: { importTable, editTable },
  data() {
    return {
      // 遮罩层
      loading: true,
      editOpen: false,
      // 唯一标识符
      uniqueId: '',
      // 选中数组
      ids: [],
      // 选中表数组
      tableNames: [],
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 表数据
      tableList: [],
      // 日期范围
      dateRange: '',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tableName: undefined,
        tableComment: undefined
      },
      // 预览参数
      preview: {
        open: false,
        title: '代码预览',
        data: {},
        activeName: 'domain.java'
      }
    }
  },
  created() {
    this.getList()
  },
  activated() {
    const time = this.$route.query.t
    if (time != null && time !== this.uniqueId) {
      this.uniqueId = time
      this.resetQuery()
    }
  },
  methods: {
    /** 查询表集合 */
    getList() {
      this.loading = true
      listTable(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.tableList = response.rows
          this.total = response.total
          this.loading = false
        }
      )
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 生成代码操作 */
    handleGenTable(row) {
      const tableNames = row.tableName || this.tableNames
      if (tableNames === '') {
        this.msgError(this.$t('请选择要生成的数据'))
        return
      }
      downLoadZip('/tool/gen/batchGenCode?tables=' + tableNames)
    },
    /** 打开导入表弹窗 */
    openImportTable() {
      this.$refs.import.show()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 预览按钮 */
    handlePreview(row) {
      previewTable(row.tableId).then(response => {
        this.preview.data = response.data
        this.preview.open = true
      })
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.tableId)
      this.tableNames = selection.map(item => item.tableName)
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleEditTable(row) {
      this.editOpen = true
      const tableId = row.tableId || this.ids[0]
      this.$nextTick(() => {
          this.$refs.editTable.init(tableId)
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const tableIds = row.tableId || this.ids
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(function() {
          return delTable(tableIds)
      }).then(() => {
          this.getList()
          this.msgSuccess(this.$t('删除成功'))
      }).catch(function() {})
    }
  }
}
</script>
